
<!doctype html>
<html lang="en" class="no-js">
  <head>
    
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      
      
      
      <link rel="icon" href="../../../../static/images/favicon.png">
      <meta name="generator" content="mkdocs-1.3.0, mkdocs-material-8.2.8">
    
    
      
        <title>EMQx 生产集群部署 - WL4G DOCS</title>
      
    
    
      <link rel="stylesheet" href="../../../../assets/stylesheets/main.644de097.min.css">
      
        
        <link rel="stylesheet" href="../../../../assets/stylesheets/palette.e6a45f82.min.css">
        
      
    
    
    
      
        
        
        <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
        <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,300i,400,400i,700,700i%7CRoboto+Mono:400,400i,700,700i&display=fallback">
        <style>:root{--md-text-font:"Roboto";--md-code-font:"Roboto Mono"}</style>
      
    
    
      <link rel="stylesheet" href="../../../../static/css/util.css">
    
    <script>__md_scope=new URL("../../../..",location),__md_get=(e,_=localStorage,t=__md_scope)=>JSON.parse(_.getItem(t.pathname+"."+e)),__md_set=(e,_,t=localStorage,a=__md_scope)=>{try{t.setItem(a.pathname+"."+e,JSON.stringify(_))}catch(e){}}</script>
    
      

    
    
  </head>
  
  
    
    
      
    
    
    
    
    <body dir="ltr" data-md-color-scheme="default" data-md-color-primary="" data-md-color-accent="">
  
    
    
      <script>var palette=__md_get("__palette");if(palette&&"object"==typeof palette.color)for(var key of Object.keys(palette.color))document.body.setAttribute("data-md-color-"+key,palette.color[key])</script>
    
    <input class="md-toggle" data-md-toggle="drawer" type="checkbox" id="__drawer" autocomplete="off">
    <input class="md-toggle" data-md-toggle="search" type="checkbox" id="__search" autocomplete="off">
    <label class="md-overlay" for="__drawer"></label>
    <div data-md-component="skip">
      
        
        <a href="#emqx" class="md-skip">
          Skip to content
        </a>
      
    </div>
    <div data-md-component="announce">
      
    </div>
    
      <div data-md-component="outdated" hidden>
        <aside class="md-banner md-banner--warning">
          
        </aside>
      </div>
    
    
      

<header class="md-header" data-md-component="header">
  <nav class="md-header__inner md-grid" aria-label="Header">
    <a href="../../../.." title="WL4G DOCS" class="md-header__button md-logo" aria-label="WL4G DOCS" data-md-component="logo">
      
  <img src="../../../../static/images/mylogo.jpeg" alt="logo">

    </a>
    <label class="md-header__button md-icon" for="__drawer">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M3 6h18v2H3V6m0 5h18v2H3v-2m0 5h18v2H3v-2z"/></svg>
    </label>
    <div class="md-header__title" data-md-component="header-title">
      <div class="md-header__ellipsis">
        <div class="md-header__topic">
          <span class="md-ellipsis">
            WL4G DOCS
          </span>
        </div>
        <div class="md-header__topic" data-md-component="header-topic">
          <span class="md-ellipsis">
            
              EMQx 生产集群部署
            
          </span>
        </div>
      </div>
    </div>
    
      <form class="md-header__option" data-md-component="palette">
        
          
          
          <input class="md-option" data-md-color-media="(prefers-color-scheme: light)" data-md-color-scheme="default" data-md-color-primary="" data-md-color-accent=""  aria-label="Switch to dark mode"  type="radio" name="__palette" id="__palette_1">
          
            <label class="md-header__button md-icon" title="Switch to dark mode" for="__palette_2" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17 6H7c-3.31 0-6 2.69-6 6s2.69 6 6 6h10c3.31 0 6-2.69 6-6s-2.69-6-6-6zm0 10H7c-2.21 0-4-1.79-4-4s1.79-4 4-4h10c2.21 0 4 1.79 4 4s-1.79 4-4 4zM7 9c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z"/></svg>
            </label>
          
        
          
          
          <input class="md-option" data-md-color-media="(prefers-color-scheme: dark)" data-md-color-scheme="slate" data-md-color-primary="" data-md-color-accent=""  aria-label="Switch to light mode"  type="radio" name="__palette" id="__palette_2">
          
            <label class="md-header__button md-icon" title="Switch to light mode" for="__palette_1" hidden>
              <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M17 7H7a5 5 0 0 0-5 5 5 5 0 0 0 5 5h10a5 5 0 0 0 5-5 5 5 0 0 0-5-5m0 8a3 3 0 0 1-3-3 3 3 0 0 1 3-3 3 3 0 0 1 3 3 3 3 0 0 1-3 3z"/></svg>
            </label>
          
        
      </form>
    
    
      <div class="md-header__option">
        <div class="md-select">
          
          <button class="md-header__button md-icon" aria-label="Select language">
            <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="m12.87 15.07-2.54-2.51.03-.03A17.52 17.52 0 0 0 14.07 6H17V4h-7V2H8v2H1v2h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04M18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12m-2.62 7 1.62-4.33L19.12 17h-3.24z"/></svg>
          </button>
          <div class="md-select__inner">
            <ul class="md-select__list">
              
                <li class="md-select__item">
                  <a href="/en/" hreflang="en" class="md-select__link">
                    English
                  </a>
                </li>
                
                <li class="md-select__item">
                  <a href="/zh/" hreflang="zh" class="md-select__link">
                    简体中文
                  </a>
                </li>
                
            </ul>
          </div>
        </div>
      </div>
    
    
      <label class="md-header__button md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
      </label>
      <div class="md-search" data-md-component="search" role="dialog">
  <label class="md-search__overlay" for="__search"></label>
  <div class="md-search__inner" role="search">
    <form class="md-search__form" name="search">
      <input type="text" class="md-search__input" name="query" aria-label="Search" placeholder="Search" autocapitalize="off" autocorrect="off" autocomplete="off" spellcheck="false" data-md-component="search-query" required>
      <label class="md-search__icon md-icon" for="__search">
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M9.5 3A6.5 6.5 0 0 1 16 9.5c0 1.61-.59 3.09-1.56 4.23l.27.27h.79l5 5-1.5 1.5-5-5v-.79l-.27-.27A6.516 6.516 0 0 1 9.5 16 6.5 6.5 0 0 1 3 9.5 6.5 6.5 0 0 1 9.5 3m0 2C7 5 5 7 5 9.5S7 14 9.5 14 14 12 14 9.5 12 5 9.5 5z"/></svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M20 11v2H8l5.5 5.5-1.42 1.42L4.16 12l7.92-7.92L13.5 5.5 8 11h12z"/></svg>
      </label>
      <nav class="md-search__options" aria-label="Search">
        
        <button type="reset" class="md-search__icon md-icon" aria-label="Clear" tabindex="-1">
          <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 6.41 17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z"/></svg>
        </button>
      </nav>
      
        <div class="md-search__suggest" data-md-component="search-suggest"></div>
      
    </form>
    <div class="md-search__output">
      <div class="md-search__scrollwrap" data-md-scrollfix>
        <div class="md-search-result" data-md-component="search-result">
          <div class="md-search-result__meta">
            Initializing search
          </div>
          <ol class="md-search-result__list"></ol>
        </div>
      </div>
    </div>
  </div>
</div>
    
    
  </nav>
  
</header>
    
    <div class="md-container" data-md-component="container">
      
      
        
          
            
<nav class="md-tabs" aria-label="Tabs" data-md-component="tabs">
  <div class="md-tabs__inner md-grid">
    <ul class="md-tabs__list">
      
        
  
  


  
  
  
    <li class="md-tabs__item">
      <a href="../../../.." class="md-tabs__link">
        Getting Started
      </a>
    </li>
  

      
    </ul>
  </div>
</nav>
          
        
      
      <main class="md-main" data-md-component="main">
        <div class="md-main__inner md-grid">
          
            
              
              <div class="md-sidebar md-sidebar--primary" data-md-component="sidebar" data-md-type="navigation" >
                <div class="md-sidebar__scrollwrap">
                  <div class="md-sidebar__inner">
                    

  


  

<nav class="md-nav md-nav--primary md-nav--lifted md-nav--integrated" aria-label="Navigation" data-md-level="0">
  <label class="md-nav__title" for="__drawer">
    <a href="../../../.." title="WL4G DOCS" class="md-nav__button md-logo" aria-label="WL4G DOCS" data-md-component="logo">
      
  <img src="../../../../static/images/mylogo.jpeg" alt="logo">

    </a>
    WL4G DOCS
  </label>
  
  <ul class="md-nav__list" data-md-scrollfix>
    
      
      
      

  
  
  
    
    <li class="md-nav__item md-nav__item--nested">
      
      
        <input class="md-nav__toggle md-toggle" data-md-toggle="__nav_1" data-md-state="indeterminate" type="checkbox" id="__nav_1" checked>
      
      
      
      
        <label class="md-nav__link" for="__nav_1">
          Getting Started
          <span class="md-nav__icon md-icon"></span>
        </label>
      
      <nav class="md-nav" aria-label="Getting Started" data-md-level="1">
        <label class="md-nav__title" for="__nav_1">
          <span class="md-nav__icon md-icon"></span>
          Getting Started
        </label>
        <ul class="md-nav__list" data-md-scrollfix>
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../.." class="md-nav__link">
        Introduction
      </a>
    </li>
  

            
          
            
              
  
  
  
    <li class="md-nav__item">
      <a href="../../../../ABOUT_CN/" class="md-nav__link">
        About
      </a>
    </li>
  

            
          
        </ul>
      </nav>
    </li>
  

    
  </ul>
</nav>
                  </div>
                </div>
              </div>
            
            
          
          <div class="md-content" data-md-component="content">
            <article class="md-content__inner md-typeset">
              
                


<h1 id="emqx">EMQx 生产集群部署<a class="headerlink" href="#emqx" title="Permanent link">&para;</a></h1>
<p>注：老版本 2.x 名称叫 emqtt，新版 4.x 叫 emqx，主配置变化不大，本文配置中就统一使用 emq 作为名称。</p>
<h2 id="1-host">1. 基于 Host 部署集群<a class="headerlink" href="#1-host" title="Permanent link">&para;</a></h2>
<ul>
<li><a href="https://docs.emqx.cn/broker/v4.3">docs.emqx.cn/broker/v4.3/</a></li>
</ul>
<h4 id="11">1.1 下载安装<a class="headerlink" href="#11" title="Permanent link">&para;</a></h4>
<div class="highlight"><pre><span></span><code><a id="__codelineno-0-1" name="__codelineno-0-1"></a><a href="#__codelineno-0-1"><span class="linenos" data-linenos="1 "></span></a><span class="c1"># 注: 对应系统版本二进制包</span>
<a id="__codelineno-0-2" name="__codelineno-0-2"></a><a href="#__codelineno-0-2"><span class="linenos" data-linenos="2 "></span></a><span class="c1"># [https://www.emqx.com/zh/downloads?product=broker](https://www.emqx.com/zh/downloads?product=broker)</span>
<a id="__codelineno-0-3" name="__codelineno-0-3"></a><a href="#__codelineno-0-3"><span class="linenos" data-linenos="3 "></span></a>
<a id="__codelineno-0-4" name="__codelineno-0-4"></a><a href="#__codelineno-0-4"><span class="linenos" data-linenos="4 "></span></a>sudo mkdir -p /opt/apps<span class="p">;</span> sudo <span class="nb">cd</span> /opt/apps
<a id="__codelineno-0-5" name="__codelineno-0-5"></a><a href="#__codelineno-0-5"><span class="linenos" data-linenos="5 "></span></a>sudo curl -OL https://www.emqx.com/zh/downloads/broker/2.3.11/emqttd-centos7-v2.3.11.zip
<a id="__codelineno-0-6" name="__codelineno-0-6"></a><a href="#__codelineno-0-6"><span class="linenos" data-linenos="6 "></span></a>sudo unzip emqttd-*.zip
<a id="__codelineno-0-7" name="__codelineno-0-7"></a><a href="#__codelineno-0-7"><span class="linenos" data-linenos="7 "></span></a>sudo ln -snf /opt/apps/emqttd-* /usr/lib/emq-current
</code></pre></div>
<h4 id="12">1.2 基础配置<a class="headerlink" href="#12" title="Permanent link">&para;</a></h4>
<ul>
<li>配置外部日志、数据目录</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-1-1" name="__codelineno-1-1"></a><a href="#__codelineno-1-1"><span class="linenos" data-linenos="1 "></span></a>sudo mkdir -p /mnt/disk1/emq
<a id="__codelineno-1-2" name="__codelineno-1-2"></a><a href="#__codelineno-1-2"><span class="linenos" data-linenos="2 "></span></a>sudo cp -r /usr/lib/emq-current/data/* /mnt/disk1/emq/ <span class="c1"># 【重要】Erlang OTP 数据库文件，否则可能起不来</span>
</code></pre></div>
<ul>
<li>将 <code>RUNNER_LOG_DIR=$RUNNER_ROOT_DIR/logs</code> 改为  <code>RUNNER_LOG_DIR=/mnt/disk1/log/emq</code></li>
<li>将 <code>RUNNER_DATA_DIR=$RUNNER_ROOT_DIR/data</code> 改为 <code>RUNNER_DATA_DIR=/mnt/disk1/emq/data</code></li>
</ul>
<h4 id="13">1.3 运行配置<a class="headerlink" href="#13" title="Permanent link">&para;</a></h4>
<ul>
<li>配置集群策略、节点、日志目录等，<code>$EMQ_HOME/etc/emq.conf</code>，以节点1为例: <a href="https://docs.emqx.cn/broker/v2.0/cluster.html#emq-r2-分布集群设计">EMQ FQDN 节点</a></li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-2-1" name="__codelineno-2-1"></a><a href="#__codelineno-2-1"><span class="linenos" data-linenos=" 1 "></span></a>## 集群相关配置
<a id="__codelineno-2-2" name="__codelineno-2-2"></a><a href="#__codelineno-2-2"><span class="linenos" data-linenos=" 2 "></span></a>cluster.name = emqcl
<a id="__codelineno-2-3" name="__codelineno-2-3"></a><a href="#__codelineno-2-3"><span class="linenos" data-linenos=" 3 "></span></a>## Default: manual
<a id="__codelineno-2-4" name="__codelineno-2-4"></a><a href="#__codelineno-2-4"><span class="linenos" data-linenos=" 4 "></span></a>cluster.discovery = static  # 【重要】默认 mamual 模式
<a id="__codelineno-2-5" name="__codelineno-2-5"></a><a href="#__codelineno-2-5"><span class="linenos" data-linenos=" 5 "></span></a>
<a id="__codelineno-2-6" name="__codelineno-2-6"></a><a href="#__codelineno-2-6"><span class="linenos" data-linenos=" 6 "></span></a>## Node list of the cluster.
<a id="__codelineno-2-7" name="__codelineno-2-7"></a><a href="#__codelineno-2-7"><span class="linenos" data-linenos=" 7 "></span></a>cluster.static.seeds = emq@10.111.0.111,emq@10.111.0.112  # 这里简单简单实用IP的方式，也可使用FQDN
<a id="__codelineno-2-8" name="__codelineno-2-8"></a><a href="#__codelineno-2-8"><span class="linenos" data-linenos=" 8 "></span></a>
<a id="__codelineno-2-9" name="__codelineno-2-9"></a><a href="#__codelineno-2-9"><span class="linenos" data-linenos=" 9 "></span></a>## Default: emqx@127.0.0.1
<a id="__codelineno-2-10" name="__codelineno-2-10"></a><a href="#__codelineno-2-10"><span class="linenos" data-linenos="10 "></span></a>node.name = emq@10.111.0.111  #【重要】
<a id="__codelineno-2-11" name="__codelineno-2-11"></a><a href="#__codelineno-2-11"><span class="linenos" data-linenos="11 "></span></a>
<a id="__codelineno-2-12" name="__codelineno-2-12"></a><a href="#__codelineno-2-12"><span class="linenos" data-linenos="12 "></span></a>## 日志相关配置
<a id="__codelineno-2-13" name="__codelineno-2-13"></a><a href="#__codelineno-2-13"><span class="linenos" data-linenos="13 "></span></a>## Crash dump log file.
<a id="__codelineno-2-14" name="__codelineno-2-14"></a><a href="#__codelineno-2-14"><span class="linenos" data-linenos="14 "></span></a>node.crash_dump = /mnt/disk1/log/emq/crash.dump
<a id="__codelineno-2-15" name="__codelineno-2-15"></a><a href="#__codelineno-2-15"><span class="linenos" data-linenos="15 "></span></a>## Sets the log dir.
<a id="__codelineno-2-16" name="__codelineno-2-16"></a><a href="#__codelineno-2-16"><span class="linenos" data-linenos="16 "></span></a>log.dir = /mnt/disk1/log/emq
<a id="__codelineno-2-17" name="__codelineno-2-17"></a><a href="#__codelineno-2-17"><span class="linenos" data-linenos="17 "></span></a>## The file where error logs will be writed to.
<a id="__codelineno-2-18" name="__codelineno-2-18"></a><a href="#__codelineno-2-18"><span class="linenos" data-linenos="18 "></span></a>log.error.file = /mnt/disk1/log/emq/error.log
<a id="__codelineno-2-19" name="__codelineno-2-19"></a><a href="#__codelineno-2-19"><span class="linenos" data-linenos="19 "></span></a>## The file for crash log.
<a id="__codelineno-2-20" name="__codelineno-2-20"></a><a href="#__codelineno-2-20"><span class="linenos" data-linenos="20 "></span></a>log.crash.file = /mnt/disk1/log/emq/crash.log
</code></pre></div>
<h4 id="14">1.4 服务配置<a class="headerlink" href="#14" title="Permanent link">&para;</a></h4>
<p><details style="cursor:pointer;;padding-left:2%">
<summary style="color:#007acc;">[展开] Configure emq.service</summary>
<code><pre>
sudo cat <<-'EOF' >/etc/systemd/system/emq.service
#!/bin/bash
# Copyright (c) 2017 ~ 2025, the original author wangl.sir individual Inc,
# All rights reserved. Contact us wanglsir&lt;wangl@gmail.com, 983708408@qq.com&gt;
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# @see: https://docs.emqx.cn/broker/v2.0/
# @see: https://blogs.wl4g.com/archives/705

[Unit]
Description=EMQ Server
After=network.target

[Install]
WantedBy=multi-user.target

[Service]
Type=forking
User=root
Group=root
ExecStart=/usr/lib/emq-current/bin/emqttd start
ExecReload=/bin/kill -s HUP
StandardOutput=journal
StandardError=journal
# Cannot set too large to exceed the maximum system limit, otherwise an error will be reported: 'Failed at
# step LIMITS spawning: Operation not permitted'. e.g: /etc/security/limits.conf
LimitNOFILE=65535
LimitNPROC=65535
LimitCORE=infinity
TimeoutStartSec=10
TimeoutSec=600
Restart=always
PermissionsStartOnly=true
RuntimeDirectoryMode=755
PrivateTmp=false
EOF

systemctl daemon-reload
systemctl enable emq
systemctl restart emq
systemctl status emq
journalctl -afu emq
</pre></code>
</details></p>

<h4 id="16">1.6 重启集群<a class="headerlink" href="#16" title="Permanent link">&para;</a></h4>
<ul>
<li>拷贝到各个节点，然后依次启动。</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-3-1" name="__codelineno-3-1"></a><a href="#__codelineno-3-1"><span class="linenos" data-linenos="1 "></span></a>ssh collect-node1 <span class="s2">&quot;sudo mkdir -p /mnt/disk1/&quot;</span>
<a id="__codelineno-3-2" name="__codelineno-3-2"></a><a href="#__codelineno-3-2"><span class="linenos" data-linenos="2 "></span></a>scp -r /mnt/disk1/emq/ collect-node2:/mnt/disk1/
<a id="__codelineno-3-3" name="__codelineno-3-3"></a><a href="#__codelineno-3-3"><span class="linenos" data-linenos="3 "></span></a>scp -r /opt/apps/emqtt* collect-node2:/opt
<a id="__codelineno-3-4" name="__codelineno-3-4"></a><a href="#__codelineno-3-4"><span class="linenos" data-linenos="4 "></span></a>ssh collect-node1 <span class="s2">&quot;sudo ln -snf /opt/apps/emqtt* /usr/lib/emqtt-current&quot;</span>
</code></pre></div>
<h4 id="17">1.7 检查成功<a class="headerlink" href="#17" title="Permanent link">&para;</a></h4>
<div class="highlight"><pre><span></span><code><a id="__codelineno-4-1" name="__codelineno-4-1"></a><a href="#__codelineno-4-1"><span class="linenos" data-linenos=" 1 "></span></a>ss -nlpt <span class="p">|</span> grep beam
<a id="__codelineno-4-2" name="__codelineno-4-2"></a><a href="#__codelineno-4-2"><span class="linenos" data-linenos=" 2 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:1883             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">27</span><span class="o">))</span>
<a id="__codelineno-4-3" name="__codelineno-4-3"></a><a href="#__codelineno-4-3"><span class="linenos" data-linenos=" 3 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>              <span class="m">127</span>.0.0.1:18083            <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">32</span><span class="o">))</span>
<a id="__codelineno-4-4" name="__codelineno-4-4"></a><a href="#__codelineno-4-4"><span class="linenos" data-linenos=" 4 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">512</span>              <span class="m">127</span>.0.0.1:11883            <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">26</span><span class="o">))</span>
<a id="__codelineno-4-5" name="__codelineno-4-5"></a><a href="#__codelineno-4-5"><span class="linenos" data-linenos=" 5 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">512</span>              <span class="m">127</span>.0.0.1:8081            <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">31</span><span class="o">))</span>
<a id="__codelineno-4-6" name="__codelineno-4-6"></a><a href="#__codelineno-4-6"><span class="linenos" data-linenos=" 6 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">128</span>                <span class="m">0</span>.0.0.0:4370             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">17</span><span class="o">))</span>
<a id="__codelineno-4-7" name="__codelineno-4-7"></a><a href="#__codelineno-4-7"><span class="linenos" data-linenos=" 7 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:8883             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">29</span><span class="o">))</span>
<a id="__codelineno-4-8" name="__codelineno-4-8"></a><a href="#__codelineno-4-8"><span class="linenos" data-linenos=" 8 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:8083             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">28</span><span class="o">))</span>
<a id="__codelineno-4-9" name="__codelineno-4-9"></a><a href="#__codelineno-4-9"><span class="linenos" data-linenos=" 9 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:8084             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">30</span><span class="o">))</span>
<a id="__codelineno-4-10" name="__codelineno-4-10"></a><a href="#__codelineno-4-10"><span class="linenos" data-linenos="10 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">5</span>                  <span class="m">0</span>.0.0.0:5369             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">22</span><span class="o">))</span>
</code></pre></div>
<ul>
<li>浏览器访问: http://127.0.0.1:18083/#/ ，默认账号/密码：admin/public</li>
<li>若在1.6.1中未启动成功，则将 <code>$EMQ_HOME/data</code> 目录拷贝至 <code>/mnt/disk1/emq/data</code>（因为在此data目录中有一些emq启动必须加装项配置）</li>
</ul>
<h2 id="2-docker">2. 基于 docker 部署集群<a class="headerlink" href="#2-docker" title="Permanent link">&para;</a></h2>
<ul>
<li>同 <a href="#1._基于_Host_部署集群">#1.基于Host部署集群</a> 选其一即可。</li>
</ul>
<h4 id="21">2.1 手动配置<a class="headerlink" href="#21" title="Permanent link">&para;</a></h4>
<ul>
<li>
<p>注：目前在<code>emqx/emqx:v4.3.10</code>版本的镜像中测试直接启动容器，然后<code>docker logs -f emqx1</code>查看日志发现会报错 <code>Failed to read: "data/loaded_modules", error: enoent</code>，看来它不会自动创建配置文件。如果不想麻烦，直接去掉<code>-v /mnt/disk1/emqx/data/:/opt/emqx/data/</code>也不会有问题了。</p>
</li>
<li>
<p>参考: <a href="https://docs.emqx.com/zh/emqx/v4.3/tutorial/prometheus.html#prometheus-%E7%9B%91%E6%8E%A7%E5%91%8A%E8%AD%A6">docs.emqx.com/zh/emqx/v4.3/tutorial/prometheus.html#prometheus-监控告警</a></p>
</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-5-1" name="__codelineno-5-1"></a><a href="#__codelineno-5-1"><span class="linenos" data-linenos=" 1 "></span></a>sudo mkdir -p /mnt/disk1/emqx/<span class="o">{</span>etc/plugins,data<span class="o">}</span>
<a id="__codelineno-5-2" name="__codelineno-5-2"></a><a href="#__codelineno-5-2"><span class="linenos" data-linenos=" 2 "></span></a>sudo chmod -R <span class="m">777</span> /mnt/disk1/emqx
<a id="__codelineno-5-3" name="__codelineno-5-3"></a><a href="#__codelineno-5-3"><span class="linenos" data-linenos=" 3 "></span></a>
<a id="__codelineno-5-4" name="__codelineno-5-4"></a><a href="#__codelineno-5-4"><span class="linenos" data-linenos=" 4 "></span></a><span class="c1"># 配置默认加载的模块</span>
<a id="__codelineno-5-5" name="__codelineno-5-5"></a><a href="#__codelineno-5-5"><span class="linenos" data-linenos=" 5 "></span></a>cat <span class="s">&lt;&lt;-&#39;EOF&#39; &gt;/mnt/disk1/emqx/data/loaded_modules</span>
<a id="__codelineno-5-6" name="__codelineno-5-6"></a><a href="#__codelineno-5-6"><span class="linenos" data-linenos=" 6 "></span></a><span class="s">{emqx_mod_acl_internal, true}.</span>
<a id="__codelineno-5-7" name="__codelineno-5-7"></a><a href="#__codelineno-5-7"><span class="linenos" data-linenos=" 7 "></span></a><span class="s">{emqx_mod_topic_metrics, true}.</span>
<a id="__codelineno-5-8" name="__codelineno-5-8"></a><a href="#__codelineno-5-8"><span class="linenos" data-linenos=" 8 "></span></a><span class="s">{emqx_mod_presence, true}.</span>
<a id="__codelineno-5-9" name="__codelineno-5-9"></a><a href="#__codelineno-5-9"><span class="linenos" data-linenos=" 9 "></span></a><span class="s">{emqx_mod_delayed, false}.</span>
<a id="__codelineno-5-10" name="__codelineno-5-10"></a><a href="#__codelineno-5-10"><span class="linenos" data-linenos="10 "></span></a><span class="s">{emqx_mod_rewrite, false}.</span>
<a id="__codelineno-5-11" name="__codelineno-5-11"></a><a href="#__codelineno-5-11"><span class="linenos" data-linenos="11 "></span></a><span class="s">{emqx_mod_subscription, false}.</span>
<a id="__codelineno-5-12" name="__codelineno-5-12"></a><a href="#__codelineno-5-12"><span class="linenos" data-linenos="12 "></span></a><span class="s">EOF</span>
<a id="__codelineno-5-13" name="__codelineno-5-13"></a><a href="#__codelineno-5-13"><span class="linenos" data-linenos="13 "></span></a>
<a id="__codelineno-5-14" name="__codelineno-5-14"></a><a href="#__codelineno-5-14"><span class="linenos" data-linenos="14 "></span></a><span class="c1"># 配置默认开启的插件</span>
<a id="__codelineno-5-15" name="__codelineno-5-15"></a><a href="#__codelineno-5-15"><span class="linenos" data-linenos="15 "></span></a>cat <span class="s">&lt;&lt;-&#39;EOF&#39; &gt;/mnt/disk1/emqx/data/loaded_plugins</span>
<a id="__codelineno-5-16" name="__codelineno-5-16"></a><a href="#__codelineno-5-16"><span class="linenos" data-linenos="16 "></span></a><span class="s">{emqx_management, true}.</span>
<a id="__codelineno-5-17" name="__codelineno-5-17"></a><a href="#__codelineno-5-17"><span class="linenos" data-linenos="17 "></span></a><span class="s">{emqx_dashboard, true}.</span>
<a id="__codelineno-5-18" name="__codelineno-5-18"></a><a href="#__codelineno-5-18"><span class="linenos" data-linenos="18 "></span></a><span class="s">{emqx_recon, true}.</span>
<a id="__codelineno-5-19" name="__codelineno-5-19"></a><a href="#__codelineno-5-19"><span class="linenos" data-linenos="19 "></span></a><span class="s">{emqx_retainer, true}.</span>
<a id="__codelineno-5-20" name="__codelineno-5-20"></a><a href="#__codelineno-5-20"><span class="linenos" data-linenos="20 "></span></a><span class="s">{emqx_telemetry, true}.</span>
<a id="__codelineno-5-21" name="__codelineno-5-21"></a><a href="#__codelineno-5-21"><span class="linenos" data-linenos="21 "></span></a><span class="s">{emqx_rule_engine, true}.</span>
<a id="__codelineno-5-22" name="__codelineno-5-22"></a><a href="#__codelineno-5-22"><span class="linenos" data-linenos="22 "></span></a><span class="s">{emqx_prometheus,true}.</span>
<a id="__codelineno-5-23" name="__codelineno-5-23"></a><a href="#__codelineno-5-23"><span class="linenos" data-linenos="23 "></span></a><span class="s">{emqx_modules, false}.</span>
<a id="__codelineno-5-24" name="__codelineno-5-24"></a><a href="#__codelineno-5-24"><span class="linenos" data-linenos="24 "></span></a><span class="s">{emqx_bridge_mqtt, false}.</span>
<a id="__codelineno-5-25" name="__codelineno-5-25"></a><a href="#__codelineno-5-25"><span class="linenos" data-linenos="25 "></span></a><span class="s">EOF</span>
<a id="__codelineno-5-26" name="__codelineno-5-26"></a><a href="#__codelineno-5-26"><span class="linenos" data-linenos="26 "></span></a>
<a id="__codelineno-5-27" name="__codelineno-5-27"></a><a href="#__codelineno-5-27"><span class="linenos" data-linenos="27 "></span></a><span class="c1"># 如上 emqx_prometheus 插件开启后会加载此配置，将 metrics 自动推送到 pushgateway</span>
<a id="__codelineno-5-28" name="__codelineno-5-28"></a><a href="#__codelineno-5-28"><span class="linenos" data-linenos="28 "></span></a>cat <span class="s">&lt;&lt;-&#39;EOF&#39; &gt;/mnt/disk1/emqx/etc/plugins/emqx_statsd.conf</span>
<a id="__codelineno-5-29" name="__codelineno-5-29"></a><a href="#__codelineno-5-29"><span class="linenos" data-linenos="29 "></span></a><span class="s">statsd.push.gateway.server=http://127.0.0.1:9091</span>
<a id="__codelineno-5-30" name="__codelineno-5-30"></a><a href="#__codelineno-5-30"><span class="linenos" data-linenos="30 "></span></a><span class="s">statsd.interval=15000</span>
<a id="__codelineno-5-31" name="__codelineno-5-31"></a><a href="#__codelineno-5-31"><span class="linenos" data-linenos="31 "></span></a><span class="s">EOF</span>
<a id="__codelineno-5-32" name="__codelineno-5-32"></a><a href="#__codelineno-5-32"><span class="linenos" data-linenos="32 "></span></a>
<a id="__codelineno-5-33" name="__codelineno-5-33"></a><a href="#__codelineno-5-33"><span class="linenos" data-linenos="33 "></span></a>sudo chmod -R <span class="m">777</span> /mnt/disk1/emqx
</code></pre></div>
<h4 id="22">2.2 启动容器<a class="headerlink" href="#22" title="Permanent link">&para;</a></h4>
<ul>
<li>参考: <a href="https://docs.emqx.com/zh/emqx/v4.3/getting-started/install.html#docker-compose-%E7%AE%80%E5%8D%95%E9%9B%86%E7%BE%A4">docs.emqx.com/zh/emqx/v4.3/getting-started/install.html#docker-compose-简单集群</a></li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-6-1" name="__codelineno-6-1"></a><a href="#__codelineno-6-1"><span class="linenos" data-linenos=" 1 "></span></a>docker run -itd <span class="se">\</span>
<a id="__codelineno-6-2" name="__codelineno-6-2"></a><a href="#__codelineno-6-2"><span class="linenos" data-linenos=" 2 "></span></a>--network<span class="o">=</span>host <span class="se">\</span>
<a id="__codelineno-6-3" name="__codelineno-6-3"></a><a href="#__codelineno-6-3"><span class="linenos" data-linenos=" 3 "></span></a>--restart<span class="o">=</span>always <span class="se">\</span>
<a id="__codelineno-6-4" name="__codelineno-6-4"></a><a href="#__codelineno-6-4"><span class="linenos" data-linenos=" 4 "></span></a>--name<span class="o">=</span>emqx1 <span class="se">\</span>
<a id="__codelineno-6-5" name="__codelineno-6-5"></a><a href="#__codelineno-6-5"><span class="linenos" data-linenos=" 5 "></span></a>-e <span class="nv">EMQX_NAME</span><span class="o">=</span>emqx <span class="se">\</span>
<a id="__codelineno-6-6" name="__codelineno-6-6"></a><a href="#__codelineno-6-6"><span class="linenos" data-linenos=" 6 "></span></a>-e <span class="nv">EMQX_HOST</span><span class="o">=</span>n1.emqx.io <span class="se">\</span>
<a id="__codelineno-6-7" name="__codelineno-6-7"></a><a href="#__codelineno-6-7"><span class="linenos" data-linenos=" 7 "></span></a>-e <span class="nv">EMQX_CLUSTER__DISCOVERY</span><span class="o">=</span>static <span class="se">\</span>
<a id="__codelineno-6-8" name="__codelineno-6-8"></a><a href="#__codelineno-6-8"><span class="linenos" data-linenos=" 8 "></span></a>-e <span class="nv">EMQX_CLUSTER__STATIC__SEEDS</span><span class="o">=</span>emqx@n1.emqx.io,emqx@n2.emqx.io <span class="se">\</span>
<a id="__codelineno-6-9" name="__codelineno-6-9"></a><a href="#__codelineno-6-9"><span class="linenos" data-linenos=" 9 "></span></a>-e <span class="nv">TZ</span><span class="o">=</span>Europe/Amsterdam <span class="se">\</span>
<a id="__codelineno-6-10" name="__codelineno-6-10"></a><a href="#__codelineno-6-10"><span class="linenos" data-linenos="10 "></span></a>-v /mnt/disk1/emqx/data/:/opt/emqx/data/ <span class="se">\</span>
<a id="__codelineno-6-11" name="__codelineno-6-11"></a><a href="#__codelineno-6-11"><span class="linenos" data-linenos="11 "></span></a>-v /mnt/disk1/emqx/etc/plugins/emqx_statsd.conf:/opt/emqx/etc/plugins/emqx_statsd.conf <span class="se">\</span>
<a id="__codelineno-6-12" name="__codelineno-6-12"></a><a href="#__codelineno-6-12"><span class="linenos" data-linenos="12 "></span></a>emqx/emqx:4.3.10
</code></pre></div>
<ul>
<li>注1：以上是 <code>static</code> 方式集群发现（为简化直接使用了 host 模式），其中 <code>n1.emqx.io</code> 是宿主机名，必须一定是 <a href="https://baike.baidu.com/item/FQDN/5102541">FQDN</a> 命名规范且网络互通，或使用 IP 也可以，否则集群发现会失败。</li>
</ul>
<h4 id="23">2.3 检查启动<a class="headerlink" href="#23" title="Permanent link">&para;</a></h4>
<ul>
<li>默认启动的插件会监听以下端口，如发现某些端口未监听，请查看<code>docker logs -f --tail 99 emqx1</code>日志分析。</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-7-1" name="__codelineno-7-1"></a><a href="#__codelineno-7-1"><span class="linenos" data-linenos=" 1 "></span></a>ss -nlpt <span class="p">|</span> grep beam
<a id="__codelineno-7-2" name="__codelineno-7-2"></a><a href="#__codelineno-7-2"><span class="linenos" data-linenos=" 2 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:1883             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">27</span><span class="o">))</span>
<a id="__codelineno-7-3" name="__codelineno-7-3"></a><a href="#__codelineno-7-3"><span class="linenos" data-linenos=" 3 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>              <span class="m">127</span>.0.0.1:18083            <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">32</span><span class="o">))</span>
<a id="__codelineno-7-4" name="__codelineno-7-4"></a><a href="#__codelineno-7-4"><span class="linenos" data-linenos=" 4 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">512</span>              <span class="m">127</span>.0.0.1:11883            <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">26</span><span class="o">))</span>
<a id="__codelineno-7-5" name="__codelineno-7-5"></a><a href="#__codelineno-7-5"><span class="linenos" data-linenos=" 5 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">512</span>              <span class="m">127</span>.0.0.1:8081            <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">31</span><span class="o">))</span>
<a id="__codelineno-7-6" name="__codelineno-7-6"></a><a href="#__codelineno-7-6"><span class="linenos" data-linenos=" 6 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">128</span>                <span class="m">0</span>.0.0.0:4370             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">17</span><span class="o">))</span>
<a id="__codelineno-7-7" name="__codelineno-7-7"></a><a href="#__codelineno-7-7"><span class="linenos" data-linenos=" 7 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:8883             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">29</span><span class="o">))</span>
<a id="__codelineno-7-8" name="__codelineno-7-8"></a><a href="#__codelineno-7-8"><span class="linenos" data-linenos=" 8 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:8083             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">28</span><span class="o">))</span>
<a id="__codelineno-7-9" name="__codelineno-7-9"></a><a href="#__codelineno-7-9"><span class="linenos" data-linenos=" 9 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">1024</span>               <span class="m">0</span>.0.0.0:8084             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">30</span><span class="o">))</span>
<a id="__codelineno-7-10" name="__codelineno-7-10"></a><a href="#__codelineno-7-10"><span class="linenos" data-linenos="10 "></span></a>LISTEN    <span class="m">0</span>         <span class="m">5</span>                  <span class="m">0</span>.0.0.0:5369             <span class="m">0</span>.0.0.0:*        users:<span class="o">((</span><span class="s2">&quot;beam.smp&quot;</span>,pid<span class="o">=</span><span class="m">197060</span>,fd<span class="o">=</span><span class="m">22</span><span class="o">))</span>
</code></pre></div>
<ul>
<li>浏览器访问: http://127.0.0.1:18083/#/ ，默认账号/密码：admin/public</li>
</ul>
<h2 id="3">3. 启用客户端认证<a class="headerlink" href="#3" title="Permanent link">&para;</a></h2>
<p>即传感器、物联网终端设备。</p>
<h4 id="31">3.1 新增账号<a class="headerlink" href="#31" title="Permanent link">&para;</a></h4>
<div class="highlight"><pre><span></span><code><a id="__codelineno-8-1" name="__codelineno-8-1"></a><a href="#__codelineno-8-1"><span class="linenos" data-linenos="1 "></span></a>vim <span class="nv">$EMQ_HOME</span>/etc/plugins/emq_auth_username.conf
<a id="__codelineno-8-2" name="__codelineno-8-2"></a><a href="#__codelineno-8-2"><span class="linenos" data-linenos="2 "></span></a>
<a id="__codelineno-8-3" name="__codelineno-8-3"></a><a href="#__codelineno-8-3"><span class="linenos" data-linenos="3 "></span></a>auth.user.1.username <span class="o">=</span> admin
<a id="__codelineno-8-4" name="__codelineno-8-4"></a><a href="#__codelineno-8-4"><span class="linenos" data-linenos="4 "></span></a>auth.user.1.password <span class="o">=</span> public
<a id="__codelineno-8-5" name="__codelineno-8-5"></a><a href="#__codelineno-8-5"><span class="linenos" data-linenos="5 "></span></a>auth.user.2.username <span class="o">=</span> <span class="nb">test</span>
<a id="__codelineno-8-6" name="__codelineno-8-6"></a><a href="#__codelineno-8-6"><span class="linenos" data-linenos="6 "></span></a>auth.user.2.password <span class="o">=</span> <span class="nb">test</span>
</code></pre></div>
<h4 id="32">3.2 禁止匿名（安全推荐）<a class="headerlink" href="#32" title="Permanent link">&para;</a></h4>
<div class="highlight"><pre><span></span><code><a id="__codelineno-9-1" name="__codelineno-9-1"></a><a href="#__codelineno-9-1"><span class="linenos" data-linenos="1 "></span></a>sed -i <span class="s1">&#39;s/mqtt.allow_anonymous = true/mqtt.allow_anonymous = false/g&#39;</span> <span class="nv">$EMQ_HOME</span>/etc/emq.conf
</code></pre></div>
<ul>
<li>使用命令行加载<code>emq_auth_username</code>插件，也可在 Dashdoard -&gt; pugins 启用加载，此插件对应 配置文件$EMQ_HOME/etc/plugins/emq_auth_username.conf。</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-10-1" name="__codelineno-10-1"></a><a href="#__codelineno-10-1"><span class="linenos" data-linenos="1 "></span></a><span class="nv">$EMQ_HOME</span>/bin/emqx_ctl plugins load emq_auth_username
</code></pre></div>
<h2 id="4-prometheus-pushgateway">4. 部署 <a href="https://prometheus.io">prometheus</a> <a href="https://github.com/prometheus/pushgateway/releases">pushgateway</a><a class="headerlink" href="#4-prometheus-pushgateway" title="Permanent link">&para;</a></h2>
<ul>
<li>由于官方提供的 <a href="https://github.com/emqx/emqx-prometheus">emqx-prometheus</a> 插件只支持将指标推送到 <a href="https://github.com/prometheus/pushgateway/releases">pushgateway</a>，参考<a href="https://docs.emqx.com/zh/emqx/v4.3/tutorial/prometheus.html">docs.emqx.com/zh/emqx/v4.3/tutorial/prometheus.html</a> ，因此必须先部署它。</li>
</ul>
<h3 id="41-host">4.1 基于 Host 编译部署<a class="headerlink" href="#41-host" title="Permanent link">&para;</a></h3>
<ul>
<li>4.1.1 源码编译</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-11-1" name="__codelineno-11-1"></a><a href="#__codelineno-11-1"><span class="linenos" data-linenos="1 "></span></a><span class="nb">cd</span> /tmp
<a id="__codelineno-11-2" name="__codelineno-11-2"></a><a href="#__codelineno-11-2"><span class="linenos" data-linenos="2 "></span></a>git clone https://github.com/prometheus/pushgateway.git
<a id="__codelineno-11-3" name="__codelineno-11-3"></a><a href="#__codelineno-11-3"><span class="linenos" data-linenos="3 "></span></a>git checkout v1.4.2
<a id="__codelineno-11-4" name="__codelineno-11-4"></a><a href="#__codelineno-11-4"><span class="linenos" data-linenos="4 "></span></a>make
<a id="__codelineno-11-5" name="__codelineno-11-5"></a><a href="#__codelineno-11-5"><span class="linenos" data-linenos="5 "></span></a>
<a id="__codelineno-11-6" name="__codelineno-11-6"></a><a href="#__codelineno-11-6"><span class="linenos" data-linenos="6 "></span></a><span class="c1"># 拷贝到目标主机 bin 目录</span>
<a id="__codelineno-11-7" name="__codelineno-11-7"></a><a href="#__codelineno-11-7"><span class="linenos" data-linenos="7 "></span></a>scp pushgateway n1.emqx.io:/bin/
</code></pre></div>
<ul>
<li>4.1.2 运行配置</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-12-1" name="__codelineno-12-1"></a><a href="#__codelineno-12-1"><span class="linenos" data-linenos="1 "></span></a>mkdir -p /etc/sysconfig/
<a id="__codelineno-12-2" name="__codelineno-12-2"></a><a href="#__codelineno-12-2"><span class="linenos" data-linenos="2 "></span></a>sudo curl -o /etc/sysconfig/pushgateway.conf <span class="s1">&#39;https://raw.githubusercontent.com/wl4g/prometheus-integration/master/prometheus/pushgateway/sysconfig/pushgateway.conf&#39;</span>
</code></pre></div>
<ul>
<li>4.1.3 配置服务</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-13-1" name="__codelineno-13-1"></a><a href="#__codelineno-13-1"><span class="linenos" data-linenos="1 "></span></a>sudo curl -o /etc/systemd/system/pushgateway.service <span class="s1">&#39;https://raw.githubusercontent.com/wl4g/prometheus-integration/master/prometheus/pushgateway/systemd/pushgateway.service&#39;</span>
<a id="__codelineno-13-2" name="__codelineno-13-2"></a><a href="#__codelineno-13-2"><span class="linenos" data-linenos="2 "></span></a>
<a id="__codelineno-13-3" name="__codelineno-13-3"></a><a href="#__codelineno-13-3"><span class="linenos" data-linenos="3 "></span></a>systemctl daemon-reload
<a id="__codelineno-13-4" name="__codelineno-13-4"></a><a href="#__codelineno-13-4"><span class="linenos" data-linenos="4 "></span></a>systemctl <span class="nb">enable</span> pushgateway
<a id="__codelineno-13-5" name="__codelineno-13-5"></a><a href="#__codelineno-13-5"><span class="linenos" data-linenos="5 "></span></a>systemctl restart pushgateway
<a id="__codelineno-13-6" name="__codelineno-13-6"></a><a href="#__codelineno-13-6"><span class="linenos" data-linenos="6 "></span></a>systemctl status pushgateway
<a id="__codelineno-13-7" name="__codelineno-13-7"></a><a href="#__codelineno-13-7"><span class="linenos" data-linenos="7 "></span></a>journalctl -afu pushgateway
</code></pre></div>
<ul>
<li>4.1.4 测试验证</li>
</ul>
<div class="highlight"><pre><span></span><code><a id="__codelineno-14-1" name="__codelineno-14-1"></a><a href="#__codelineno-14-1"><span class="linenos" data-linenos="1 "></span></a>curl localhost:9091/metrics
</code></pre></div>
<h3 id="42-docker">4.2 基于 docker 部署<a class="headerlink" href="#42-docker" title="Permanent link">&para;</a></h3>
<div class="highlight"><pre><span></span><code><a id="__codelineno-15-1" name="__codelineno-15-1"></a><a href="#__codelineno-15-1"><span class="linenos" data-linenos="1 "></span></a>docker run -d --name pushgateway1 --restart always -p <span class="m">9091</span>:9091 prom/pushgateway:v1.4.2
</code></pre></div>
<h3 id="43-grafana">4.3 导入 Grafana 仪表盘<a class="headerlink" href="#43-grafana" title="Permanent link">&para;</a></h3>
<ul>
<li>参考官方提供的表盘: <a href="https://github.com/emqx/emqx-prometheus/tree/master/grafana_template">github.com/emqx/emqx-prometheus/tree/master/grafana_template</a></li>
</ul>
<h2 id="5-faq">5. FAQ<a class="headerlink" href="#5-faq" title="Permanent link">&para;</a></h2>
<h4 id="51-emqx-tz">5.1 启动 emqx 容器时指定了环境变量<code>TZ</code>不生效，还需手动进入容器里改。<a class="headerlink" href="#51-emqx-tz" title="Permanent link">&para;</a></h4>
<div class="highlight"><pre><span></span><code><a id="__codelineno-16-1" name="__codelineno-16-1"></a><a href="#__codelineno-16-1"><span class="linenos" data-linenos="1 "></span></a>docker <span class="nb">exec</span> -it emqx1 bash
<a id="__codelineno-16-2" name="__codelineno-16-2"></a><a href="#__codelineno-16-2"><span class="linenos" data-linenos="2 "></span></a>
<a id="__codelineno-16-3" name="__codelineno-16-3"></a><a href="#__codelineno-16-3"><span class="linenos" data-linenos="3 "></span></a>sudo apk add --no-cache tzdata
<a id="__codelineno-16-4" name="__codelineno-16-4"></a><a href="#__codelineno-16-4"><span class="linenos" data-linenos="4 "></span></a>sudo ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
<a id="__codelineno-16-5" name="__codelineno-16-5"></a><a href="#__codelineno-16-5"><span class="linenos" data-linenos="5 "></span></a>sudo <span class="nb">echo</span> <span class="s2">&quot;Asia/Shanghai&quot;</span> &gt; /etc/timezone
</code></pre></div>

              
            </article>
          </div>
        </div>
        
      </main>
      
        <footer class="md-footer">
  
  <div class="md-footer-meta md-typeset">
    <div class="md-footer-meta__inner md-grid">
      <div class="md-copyright">
  
  
    Made with
    <a href="https://squidfunk.github.io/mkdocs-material/" target="_blank" rel="noopener">
      Material for MkDocs
    </a>
  
</div>
      
    </div>
  </div>
</footer>
      
    </div>
    <div class="md-dialog" data-md-component="dialog">
      <div class="md-dialog__inner md-typeset"></div>
    </div>
    <script id="__config" type="application/json">{"base": "../../../..", "features": ["search.suggest", "search.highlight", "navigation.tabs", "navigation.expand", "toc.follow", "toc.integrate"], "search": "../../../../assets/javascripts/workers/search.5e67fbfe.min.js", "translations": {"clipboard.copied": "Copied to clipboard", "clipboard.copy": "Copy to clipboard", "search.config.lang": "en", "search.config.pipeline": "trimmer, stopWordFilter", "search.config.separator": "[\\s\\-]+", "search.placeholder": "Search", "search.result.more.one": "1 more on this page", "search.result.more.other": "# more on this page", "search.result.none": "No matching documents", "search.result.one": "1 matching document", "search.result.other": "# matching documents", "search.result.placeholder": "Type to start searching", "search.result.term.missing": "Missing", "select.version.title": "Select version"}, "version": {"default": "latest", "provider": "mike"}}</script>
    
    
      <script src="../../../../assets/javascripts/bundle.c44cc438.min.js"></script>
      
        <script src="../../../../static/js/util.js"></script>
      
    
  </body>
</html>